//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.service;

import cn.hutool.core.util.StrUtil;
import com.example.entity.Account;
import com.example.entity.President;
import com.example.exception.CustomException;
import com.example.mapper.PresidentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class PresidentService {
    @Resource
    private PresidentMapper presidentMapper;

    public PresidentService() {
    }

    public void add(President president) {
        String username = president.getUsername();
        President dbpresident = this.presidentMapper.selectByUsername(username);
        if (dbpresident != null) {
            throw new CustomException("500", "用户名已存在，请更换其他用户名");
        }  else {
                if (StrUtil.isBlank(president.getPassword())) {
                    president.setPassword("123"); // 默认密码 123
                }

                if (StrUtil.isBlank(president.getName())) {
                    president.setName(president.getUsername());
                }

                president.setRole("PRE");  //必须设置role
                this.presidentMapper.insert(president);
        }
    }

    public void updateById(President president) {
        this.presidentMapper.updateById(president);
    }

    public void deleteById(Integer id) {
        this.presidentMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        Iterator var2 = ids.iterator();

        while(var2.hasNext()) {
            Integer id = (Integer)var2.next();
            this.deleteById(id);
        }

    }

    public President selectById(Integer id) {
        return this.presidentMapper.selectById(id);
    }

    public List<President> selectAll(President president) {
        return this.presidentMapper.selectAll(president);
    }

    public List<President> selectList(President president) {
        System.out.println(president);
        return null;
    }

    public PageInfo<President> selectPage(President president, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<President> list = this.presidentMapper.selectAll(president);

        // Iterate through the list and set phone from contactInfo
        for (President p : list) {
            if (p.getContactInfo() != null) {
                p.setPhone(p.getContactInfo());
            }
        }

        return PageInfo.of(list);
    }

    public President login(Account account) {
        String username = account.getUsername();
        President dbpresident = this.presidentMapper.selectByUsername(username);
        if (dbpresident == null) {
            throw new CustomException("500", "用户不是社长");
        } else {
            String password = account.getPassword();
            if (!dbpresident.getPassword().equals(password)) {
                throw new CustomException("500", "密码错误，请重新输入");
            } else {
                return dbpresident;
            }
        }
    }

    public Account register(President president) {
        this.add(president);
        return president;
    }

    public void updatePassword(Account account) {
        Integer id = account.getId();
        President dbpresident = this.selectById(id);
        if (!dbpresident.getPassword().equals(account.getPassword())) {  //最不可能错的放在对比的左侧。如果数据库中的密码和提交表单的密码不一致
            throw new CustomException("500", "原密码错误");
        } else {
            dbpresident.setPassword(account.getNewPassword());
            this.updateById(dbpresident);
        }
    }

}
